package com.adui.mgt.performance.api;

import com.adui.mgt.performance.common.Result;
import com.adui.mgt.performance.entity.Role;
import com.adui.mgt.performance.entity.UserRole;
import com.adui.mgt.performance.service.RoleService;
import com.adui.mgt.performance.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 角色crud + 人员角色设置
 */
@RestController
@RequestMapping("/api/roles")
public class RoleApi {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserRoleService userRoleService;

    @PostMapping("/users")
    public Result findUsers(@RequestBody Role role) {
        return roleService.findRoles(role);
    }

    @GetMapping("/exist-flag")
    public Result existRole(@RequestParam("roleCode") String roleCode) {
        return Result.data(roleService.existRoleCodeFlag(roleCode));
    }

    @GetMapping("/{id}")
    public Result findRoleById(@PathVariable("id") String id) {
        return Result.data(roleService.findRoleById(id));
    }

    @PostMapping("")
    public Result insertRole(@RequestBody Role role) {
        return roleService.insertRole(role);
    }

    @PutMapping("")
    public Result updateRole(@RequestBody Role role) {
        return roleService.updateRole(role);
    }

    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable("id") String id) {
        return roleService.deleteRole(id);
    }

    /**
     * 给人加角色
     * @param userRole
     * @return
     */
    @PostMapping("role/users")
    public Result saveUserRoleRel(@RequestBody UserRole userRole) {

        userRoleService.saveUserRoleRel(userRole);
        return Result.ok();
    }
}
